package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.BitProblem;

/**
 * 136 137 260
 * exactly two elements appear only once and all the other elements appear exactly twice
 *
 * @author tzp
 * @since 2020/11/13
 */
public class LC260 implements BitProblem {
    public int[] singleNumber(int[] nums) {
        int diff = 0;
        for (int num : nums) {
            diff ^= num;
        }
        diff = diff & -diff;//只保留最右侧的1
        // 通过这个拆封两半, 和哈希分组一样, 然后用LC136的异或即可得到答案

        int[] rets = {0, 0};
        for (int num : nums) {
            if ((num & diff) == 0) {
                rets[0] ^= num;
            } else {
                rets[1] ^= num;
            }
        }
        return rets;
    }

    public static void main(String[] args) {
        System.out.println(new LC260());
    }
}
